import {
  api,
  util,
  forms,
  dialog
} from '../../../js/util';
import {
  fee
} from '../../../js/modules/order';
import {
  invoice
} from '../../../js/modules/store';
import {
  router
} from '../../../js/router';
import { alert } from '../../../js/newUtil';
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  behaviors: [componentBehavior],
  lifetimes: {
    attached() {
      this.invoiceAddCom = this.selectComponent("#invoiceAddCom");
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {
    invoiceInfo: {
      type:Object,
      value:{
        TaxpayerType: 0,
        InvPTTitle: '',
        InvoPTCode: '',
        InvoZZName: '',
        InvoZZCode: '',
        InvoZZAddress: '',
        InvoZZTel: '',
        InvoZZBank: '',
        InvoZZAccount: '',
        InvoZZPostAddress: '',
        InvoPTContent: '明细'
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    invoice: "<Invoice><InvType>0</InvType></Invoice>",
    NowInvoiceRate: 0, //当前发票税率
    invoiceRate: {
      ZZInvoiceRate: 0, //获取增值税发票税率
      PTInvoiceRate: 0, //获取普通发票税率 
      IsInvoice: false,
      FreightMode: 1, //订单运费计算模式：1标准模式  2商品指定运费模式   
    },
    InvoiceType: 0,
    pillTypeItems: [
      {
        value: '个人',
        checked: 'checked'
      },
      {
        value: '单位'
      }
    ],
    pillContItems: [
      {
        value: '明细',
        checked: 'checked'
      },
      {
        value: '办公用品'
      },
      {
        value: '配件'
      },
      {
        value: '耗材'
      },
      {
        value: '其他'
      }
    ],
    invoiceId: 0,
    invoiceList: [],
    invoiceIndex: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 初始化
     */
    initInvoice(invoiceInfo, invoiceType){
      this.setData({InvoiceType: invoiceType});
      if (invoiceType==1){
        this.changePillType({ detail: {value: invoiceInfo.TaxpayerType == 1 ? "单位" : "个人"}});
      }
    },
    /**
     * 发票切换
     */
    onSwitchInvoiceEvent(invoiceType) {
      let that = this;
      let {InvoiceType, invoiceInfo, pillTypeItems, pillContItems} = this.data;
      if (invoiceType!=InvoiceType) {
        invoiceInfo = {
          TaxpayerType: 0,
          InvPTTitle: '',
          InvoPTCode: '',
          InvoZZName: '',
          InvoZZCode: '',
          InvoZZAddress: '',
          InvoZZTel: '',
          InvoZZBank: '',
          InvoZZAccount: '',
          InvoZZPostAddress: '',
          InvoPTContent: '明细'
        };
      }
      if (invoiceType != 2) {
        pillTypeItems.map(function (item, index) {
          item.checked = index == invoiceInfo.TaxpayerType;
        });
        pillContItems.map(function (item) {
          item.checked = item.value == invoiceInfo.InvoPTContent;
        });
      }
      let data = {InvoiceType: invoiceType, invoiceInfo: invoiceInfo, pillTypeItems: pillTypeItems, pillContItems: pillContItems};
      this.setData(data, () => {
        that.loadInvoiceList();
      });
      dialog.showModal(this, 'showModalStatus')
    },
    /**加载发票列表 */
    loadInvoiceList() {
      let that = this;
      var InvoiceType = this.data.InvoiceType;
      var invoiceInfo = this.data.invoiceInfo;
      invoice.invoiceList(InvoiceType,'',function (content) {
        let invoiceList = content || [];
        let invoiceIndex = 0;
        if (invoiceList.length > 0) {
          let defaults = invoiceList.filter((item, index) => {
            if ((invoiceInfo.InvoPTCode && item.Code == invoiceInfo.InvoPTCode) || (!invoiceInfo.InvoPTCode && item.IsDefault == 1)) {
              invoiceIndex = index;
              return true;
            } 
            return false;
          });
          if (defaults.length==0) {
            defaults.push(invoiceList[0]);
          }
          if (InvoiceType==2) {
            if (!invoiceInfo.InvoZZName || !invoiceInfo.InvoZZCode) {
              invoiceInfo.InvoZZName = defaults[0].Name || '';
              invoiceInfo.InvoZZCode = defaults[0].Code || '';
              invoiceInfo.InvoZZAddress = defaults[0].Address || '';
              invoiceInfo.InvoZZTel = defaults[0].Tel || '';
              invoiceInfo.InvoZZBank = defaults[0].Bank || '';
              invoiceInfo.InvoZZAccount = defaults[0].Account || '';
              invoiceInfo.InvoZZPostAddress = defaults[0].PostAddress || '';
            }
          }else if (invoiceInfo.TaxpayerType==1) {
            if (!invoiceInfo.InvPTTitle || !invoiceInfo.InvoPTCode) {
              invoiceInfo.InvPTTitle = defaults[0].Name || '';
              invoiceInfo.InvoPTCode = defaults[0].Code || '';
            }
          }
        }
        that.setData({
          invoiceIndex: invoiceIndex,
          invoiceList: invoiceList,
          invoiceInfo: invoiceInfo
        });
      });
    },
    /**
     * 普通发票
     */
    changePillType: function (e) {
      var InvoiceType = this.data.InvoiceType;
      var invoiceInfo = this.data.invoiceInfo;
      var invoiceList = this.data.invoiceList;
      this.data.pillTypeItems.map(function (item) {
        if (item.value == e.detail.value) {
          item.checked = true;
        } else {
          item.checked = false;
        }
      });
      if (e.detail.value == '单位') {
        invoiceInfo.TaxpayerType = 1;
      } else {
        invoiceInfo.TaxpayerType = 0;
      }
      let invoiceIndex = 0;
      if (invoiceList.length > 0) {
        let defaults = invoiceList.filter((item, index) => {
          if (item.IsDefault == 1) {
            invoiceIndex = index;
            return true;
          } 
          return false;
        });
        if (defaults.length==0) {
          defaults.push(invoiceList[0]);
        }
        if (InvoiceType==2) {
          invoiceInfo.InvoZZName = defaults[0].Name || '';
          invoiceInfo.InvoZZCode = defaults[0].Code || '';
          invoiceInfo.InvoZZAddress = defaults[0].Address || '';
          invoiceInfo.InvoZZTel = defaults[0].Tel || '';
          invoiceInfo.InvoZZBank = defaults[0].Bank || '';
          invoiceInfo.InvoZZAccount = defaults[0].Account || '';
          invoiceInfo.InvoZZPostAddress = defaults[0].PostAddress || '';
        }else if (invoiceInfo.TaxpayerType==1) {
          invoiceInfo.InvPTTitle = defaults[0].Name || '';
          invoiceInfo.InvoPTCode = defaults[0].Code || '';
        }else{
          invoiceInfo.InvPTTitle = '';
          invoiceInfo.InvoPTCode = '';
        }
      }
      this.setData({
        invoiceIndex: invoiceIndex,
        invoiceInfo: invoiceInfo,
        pillTypeItems: this.data.pillTypeItems
      })
    },
    /**
    * 发票内容
    */
    changeInvContentType: function (e) {
      this.data.pillContItems.map(function (item) {
        if (item.value == e.detail.value) {
          item.checked = true;
        } else {
          item.checked = false;
        }
      });
      this.data.invoiceInfo.InvoPTContent = e.detail.value;
      this.setData({
        pillContItems: this.data.pillContItems
      })
    },
    /**发票的文本框触发 */
    invoInput(e) {
      var invoiceInfo = this.data.invoiceInfo;
      invoiceInfo[e.currentTarget.id] = e.detail.value;
      this.setData({
        invoiceInfo: invoiceInfo
      });
    },
    /**
     * 改变发票
     */
    bindInvoiceChange:function(e){
      var invoiceIndex = e.detail.value;
      var InvoiceType = this.data.InvoiceType;
      var invoiceInfo = this.data.invoiceInfo;
      var invoiceList = this.data.invoiceList;
      if (invoiceList.length > 0) {
        if (InvoiceType==2) {
          invoiceInfo.InvoZZName = invoiceList[invoiceIndex].Name || '';
          invoiceInfo.InvoZZCode = invoiceList[invoiceIndex].Code || '';
          invoiceInfo.InvoZZAddress = invoiceList[invoiceIndex].Address || '';
          invoiceInfo.InvoZZTel = invoiceList[invoiceIndex].Tel || '';
          invoiceInfo.InvoZZBank = invoiceList[invoiceIndex].Bank || '';
          invoiceInfo.InvoZZAccount = invoiceList[invoiceIndex].Account || '';
          invoiceInfo.InvoZZPostAddress = invoiceList[invoiceIndex].PostAddress || '';
        }else{
          invoiceInfo.InvPTTitle = invoiceList[invoiceIndex].Name || '';
          invoiceInfo.InvoPTCode = invoiceList[invoiceIndex].Code || '';
        }
      }
      this.setData({
        invoiceIndex: invoiceIndex,
        invoiceInfo: invoiceInfo
      });
    },
    bindAddInvo(e){
      var InvoiceType = this.data.InvoiceType;
      var invoiceInfo = {
        Id: 0,
        InvType: InvoiceType,
        Name: '',
        Code: '',
        Address: '',
        Tel: '',
        Bank: '',
        Account: '',
        PostAddress: '',
        Status: 1,
        IsDefault: 0,
        IsInvoiceType: false
      }
      this.invoiceAddCom.showAddModal(invoiceInfo);
    },
    /**隐藏发票弹层 */
    hideInvoModal() {
      let invoice = "<Invoice><InvType>0</InvType></Invoice>";
      let dic = {
        invoiceInfo: {
          TaxpayerType: 0,
          InvPTTitle: '',
          InvoPTCode: '',
          InvoZZName: '',
          InvoZZCode: '',
          InvoZZAddress: '',
          InvoZZTel: '',
          InvoZZBank: '',
          InvoZZAccount: '',
          InvoZZPostAddress: '',
          InvoPTContent: '明细'
        },
        invoice: invoice,
        InvoiceType: 0,
        NowInvoiceRate: 0
      };
      this.data.invoice = invoice;
      this.triggerEvent("onInvoiceCallbackEvent", { content: dic });
      dialog.hideModal(this, 'showModalStatus')
    },
    /**保存普通发票 */
    savePTInvo() {
      var invoiceInfo = this.data.invoiceInfo;
      if (invoiceInfo.InvPTTitle == null || invoiceInfo.InvPTTitle == "") {
        util.tips("请填写抬头");
        return;
      }
      if (invoiceInfo.TaxpayerType == 1 && (invoiceInfo.InvoPTCode == null || invoiceInfo.InvoPTCode == "")) {
        util.tips("请填写税号");
        return;
      }
      var invoice = fee.savePTInvo(invoiceInfo);
      this.data.invoice=invoice;
      //this.calInvRate();
      this.triggerEvent("onInvoiceCallbackEvent", { content: { invoiceInfo: invoiceInfo, invoice: invoice }});
      dialog.hideModal(this, 'showModalStatus')
    },
    /**保存增值税发票 */
    saveZZInvo() {
      var invoiceInfo = this.data.invoiceInfo;
      if (invoiceInfo.InvoZZName == null || invoiceInfo.InvoZZName == "") {
        util.tips("请填写单位名称");
        return;
      }
      if (invoiceInfo.InvoZZCode == null || invoiceInfo.InvoZZCode == "") {
        util.tips("请填写税号");
        return;
      }
      // if (invoiceInfo.InvoZZAddress == null || invoiceInfo.InvoZZAddress == "") {
      //   util.tips("请填写单位地址");
      //   return;
      // }
      // if (invoiceInfo.InvoZZTel == null || invoiceInfo.InvoZZTel == "") {
      //   util.tips("请填写单位电话");
      //   return;
      // }
      var mobileItem = {
        value: util.trim(invoiceInfo.InvoZZTel),
        validate: "tel",
        error: "请输入正确的电话号码"
      }
      if (!forms.validate(mobileItem)) return false;
      // if (invoiceInfo.InvoZZBank == null || invoiceInfo.InvoZZBank == "") {
      //   util.tips("请填写开户银行");
      //   return;
      // }
      // if (invoiceInfo.InvoZZAccount == null || invoiceInfo.InvoZZAccount == "") {
      //   util.tips("请填写银行账户");
      //   return;
      // }
      // if (invoiceInfo.InvoZZPostAddress == null || invoiceInfo.InvoZZPostAddress == "") {
      //   util.tips("请填写寄送地址");
      //   return;
      // }
      var invoice = fee.saveZZInvo(invoiceInfo);
      this.data.invoice = invoice;
      this.triggerEvent("onInvoiceCallbackEvent", { content: { invoiceInfo: invoiceInfo, invoice: invoice } });
      dialog.hideModal(this, 'showModalStatus')
    },
    /**发票回调 */
    onInvoiceCallbackEvent(e){
      this.loadInvoiceList();
    },
    /**
     * 没有可选发票时提示
     */
    showTip () {
      alert('暂无发票信息，请添加')
    },
  }
})
